<?php
include_once('../includes/definer.php');
include_once(INC.'php/bootstrap.php');
$orderColumn = array(
    'p.orden',
    'p.descripcion',
    'p.url',
    'e.value',
    'p.clicks',
    'p.impresiones',
    'p.id'
);
$desde = $_GET['length'] * $_GET['start'];

$recordsTotal = Doctrine_Query::create()
        ->select('count(p.id)')
        ->from('Publicidad p')
        ->where('p.id_estado <> '.Estado::BORRADO)
        ->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR)
;
//<a href=\"producto/", p.slug, "\" class=\"btn btn-primary\"><i class=\"fa fa-pencil\"></i></a>
$data = Doctrine_Query::create()
        /*->select('
            p.id,
            CONCAT("row", p.id) as DT_RowId,
            CONCAT("
                <a href=\"#\" data-id=\"",p.id,"\" class=\"btn btn-danger borrarPublicidad\"><i class=\"fa fa-trash-o\"></i></a>
                <a href=\"publicidad/",p.slug,"\" class=\"btn btn-primary\"><i class=\"fa fa-pencil\"></i></a>"
            ) as acciones,
            CONCAT("<a href=\"#\" class=\"descripcionEditable\" data-value=\"",p.descripcion,"\" data-publicidad=\"",p.id,"\" data-type=\"text\">",p.descripcion,"</a>") as descripcion,
            CONCAT("<a href=\"#\" class=\"linkEditable\" data-value=\"",p.url,"\" data-publicidad=\"",p.id,"\" data-type=\"text\">",p.url,"</a>") as link,
            CONCAT("<a href=\"#\" class=\"estadoEditable\" data-value=\"",e.id,"\" data-publicidad=\"",p.id,"\" data-type=\"select\">",e.value,"</a>") as estado,
            p.clicks as clicks,
            p.impresiones as impresiones
        ')*/
        ->select('
            p.id,
            CONCAT("row", p.id) as DT_RowId,
            CONCAT("
                <a href=\"#\" data-id=\"",p.id,"\" class=\"btn btn-danger borrarPublicidad\"><i class=\"fa fa-trash-o\"></i></a>
                <a href=\"publicidad/",p.slug,"\" class=\"btn btn-primary\"><i class=\"fa fa-pencil\"></i></a>"
            ) as acciones,
            CONCAT("<input type=\"text\" class=\"orden\" name=\"orden\" data-publicidad=\"",p.id,"\" value=\"",p.orden,"\" />") as orden,
            p.descripcion as descripcion,
            p.url as link,
            CONCAT("<a href=\"#\" class=\"estadoEditable\" data-value=\"",e.id,"\" data-publicidad=\"",p.id,"\" data-type=\"select\">",e.value,"</a>") as estado,
            group_concat(c.value separator ", ") as categorias,
            p.clicks as clicks,
            p.impresiones as impresiones
        ')
        ->from('Publicidad p')
        ->innerJoin('p.estado e WITH e.id <> '.Estado::BORRADO)
        ->leftJoin('p.categorias as c')
        ->limit($_GET['length'])
        ->offset($_GET['start'])
        ->orderBy($orderColumn[$_GET['order'][0]['column']].' '.$_GET['order'][0]['dir'])
        ->groupBy('p.id')
;

//busqueda
if ($_GET['search']['value']) {
    $data->andWhere('p.descripcion like "%'.$_GET['search']['value'].'%"');
}
//fin busqueda
$q2 = $data->copy();
$q2->removeDqlQueryPart('select')
        ->removeDqlQueryPart('limit')
        ->removeDqlQueryPart('offset')
        ->groupBy('1=1')
        ->addSelect('count(p.id)')
;
$recordsFiltered = $q2->execute(array(), Doctrine::HYDRATE_SINGLE_SCALAR);

//echo(json_encode($productos)); exit();
$restul = array(
    'recordsTotal'=>$recordsTotal,
    'recordsFiltered'=>$recordsFiltered,
    'data'=>$data->execute(array(), Doctrine::HYDRATE_ARRAY)
);
echo(json_encode($restul));
?>